package com.xiaoxin.dubbo.utils;

import com.xiaoxin.model.mongo.Friend;
import com.xiaoxin.model.mongo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xiaoxiaode
 * @date 2021-09-03-21:01
 **/
@Component
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    public  void saveTimeLine(Long userId, ObjectId movementId){
        Criteria criteria=Criteria.where("userId").is(userId);
        Query query=Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        // 循环好友数据,构建时间线程数据存入数据库
       /* try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        for (Friend friend : friends) {
            MovementTimeLine movementTimeLine=new MovementTimeLine();
            movementTimeLine.setCreated(System.currentTimeMillis());
            movementTimeLine.setFriendId(friend.getFriendId());
            movementTimeLine.setUserId(friend.getUserId());
            movementTimeLine.setMovementId(movementId);
            mongoTemplate.save(movementTimeLine);
        }
    }
}
